package com.dj.dormitory.handler;

import com.dj.dormitory.commonutils.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author DJ同学
 * @Date 2021/1/28 11:33
 * @Version 1.0
 */
@ControllerAdvice //全局异常处理注解
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.error().message("执行了全局异常处理。。。");
    }

    @ExceptionHandler(DormitoryException.class) //自定义异常
    @ResponseBody
    public Result error(DormitoryException e){
        e.printStackTrace();
        return Result.error().code(e.getCode()).message(e.getMsg());
    }
}
